<template>
    <div class="dataContainer">
      <!-- Search start -->
      <el-form :inline="true" size="small" :model="queryParams" class="demo-form-inline searchContainer">
        <el-form-item label="手机号: ">
          <!-- <el-input v-model="queryParams.studentID" placeholder="请选择学员"  @keyup.enter="onSearch"  style="width: 180px;"/> -->
          <el-select
            filterable
            v-model="queryParams.studentID"
            placeholder="请选择手机号" 
            style="width: 180px;"
            clearable
          >
            <el-option
              v-for="item in studentList"
              :key="item.id"
              :label="item.code+'-'+item.wechatNickname+'-'+item.phone"
              :value="item.id"
            >
                <div>
                 <div style="display: inline-block;width: 200px;">
                  <span title="学员编号">{{ item.code }}</span>
                  <span title="微信昵称" style="float:right;color:#c7c7c7;margin-right: 5px;">{{ item.wechatNickname }}</span>
                 </div>
                 <span title="手机号码" style="display: inline-block;color: var(--el-color-primary);width: 90px;">{{ item.phone }}</span>
                </div>
            </el-option>
                
            </el-select>
        </el-form-item>
        <el-form-item label="购课类型: ">
          <el-select
              v-model="queryParams.type"
              placeholder="购课类型" 
              style="width: 150px;"
              clearable
          >
              <el-option
              v-for="item in dictionaryTypeList"
              :key="item.dicValue"
              :label="item.dicName"
              :value="item.dicValue"
              />
          </el-select>
        </el-form-item>
        <el-form-item label="购课日期: ">
          <el-date-picker
                style="width: 180px;"
                v-model="queryParams.purchaseDate"
                type="date"
                placeholder="请选择购课日期"
                size="small"
                value-format="YYYY-MM-DD"
            />
        </el-form-item>
        <el-form-item>
          <el-button class="bs-form-btn" size="small" type="primary" @click="onSearch" ><el-icon><Search /></el-icon>查 询</el-button>
          <ResetButton :queryParams="queryParams"/>
          <el-button size="small" type="primary" @click="openAddModal"><el-icon><Plus /></el-icon>新增 </el-button>
        </el-form-item>
      </el-form>
      <!-- Search end -->
  
      <!-- Table start -->
      <div >
        <ColumnSetting v-model:columnList="columnList" />
        <el-table
          ref="tableRef"
          v-loading="tableLoading"
          :border="true"
          class="bs-table"
          :data="tableData"
          row-key="id"
          :height="tableHeight"
          :header-cell-style="{textAlign: 'center'}"
          :cell-style="{textAlign: 'center'}"
        >
          <el-table-column type="index" label="序号" width="80" fixed/>
          <template v-for="item in columnList" :key="item.prop">
            <el-table-column :prop="item.prop" :label="item.label"  :fixed="item.fixed" :sortable:="item.sortable" v-if="!item.hide && item.prop!='action'" :width="item.width">
              <template #default="scope">
                <template v-if="item.prop=='vipServer'">
                  {{ getVipServerName(scope.row.vipServer) }}
                </template>
                <template v-else>
                    <template v-if="item.prop=='remark'">
                        <el-tooltip class="box-item" effect="dark" :content="scope.row[item.prop]"
                        :teleported="true" :width="600" popper-class="pc">
                            <div class="multiRowText" style="width: 160px;margin: unset;">{{ scope.row[item.prop] }}</div>
                        </el-tooltip>
                    </template>
                    <template v-else-if="item.prop=='studentName'">
                      {{ scope.row.code }} - {{ scope.row.studentName }}
                    </template>
                    <template v-else>
                        {{ scope.row[item.prop] }}
                    </template>
                </template>
              </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" :width="item.width" v-else-if="!item.hide && item.prop=='action'">
               <template #default="scope">
                <el-button  size="small" link type="success"  @click="editRow(scope.row.id)" ><el-icon><Edit /></el-icon> 修改</el-button>
                <el-button  size="small" link type="danger" @click="deleteRow(scope.row.id)" ><el-icon><Delete /></el-icon> 删除</el-button>
              </template>
            </el-table-column>
          </template>
        </el-table>
        <pagination
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          small="small"
          disabled="disabled"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableTotal"
          @pagination="selectByPage"
        />
      </div>
      <AddStudent ref="addOrUpdateRef" @flushTable="selectByPage"  />
    </div>
  </template>
  <script>
  import { selectByPage, addPurchaseCourses, updatePurchaseCourses, deletePurchaseCourses } from '@/api/oss/purchaseCourses.js'
  import { getAll } from '@/api/oss/student.js'
  import { mapState } from 'vuex'
  import AddStudent from './addOrUpdate.vue'
  import { selectDictionaryByTypeCode } from '@/api/system/dictionary.js'
  import { ElLoading } from 'element-plus'
  export default {
      components:{
       AddStudent
      },
      data() {
          return {
              tableTotal: 0,
              tableLoading: false,
              tableData: [],
              // 查询参数
              queryParams: {
                  pageNo: 1,
                  pageSize: 500,
                  studentID: '',
                  type:'',
                  purchaseDate:'',
                  vipServer:'',
              },
              columnList:[
                {prop:'studentPhone',label:'手机号',sortable:false,sort:0,hide:false,fixed:true,width:150},
                {prop:'studentName',label:'学员名称',sortable:false,sort:0,hide:false,fixed:true,width:150},
                {prop:'type',label:'购课类型',sortable:false,sort:0,hide:false,fixed:false,width:200},
                {prop:'purchaseDate',label:'购课日期',sortable:false,sort:0,hide:false,fixed:false,width:150},
                {prop:'consumeAmout',label:'消费金额',sortable:false,sort:0,hide:false,fixed:false,width:150},
                {prop:'coreCoin',label:'重心币份额',sortable:false,sort:0,hide:false,fixed:false,width:150},
                {prop:'remark',label:'备注',sortable:false,sort:0,hide:false,fixed:false,width:180, showOverflowTooltip:true},
                {prop:'action',label:'操作',sortable:false,sort:0,hide:false,fixed:'right',width:200},
              ],
              tableHeight:(window.innerHeight - 380) + 'px',
              // 购课类型字典列表
              dictionaryTypeList:[],
              // 学员列表
              studentList:[],
              // 一对一服务字典列表
              vipServerList:[],
          }
      },
      created() {
        // 列的sort字段按照index赋值
        this.columnList = this.columnList.map((item,index)=>{
          item.sort = index+1
          return item
        })
        this.selectByPage()
      },
      computed:{
          ...mapState(['amplify'])
      },
      mounted(){
          this.$nextTick(()=>{
              this.countTableHeight()
              selectDictionaryByTypeCode({"dicTypeCode":"PurchaseCoursesType"}).then(res=>{
                  if (res.code == 1) {
                      this.dictionaryTypeList = res.data
                  }
              })
              // selectDictionaryByTypeCode({"dicTypeCode":"VipServer"}).then(res=>{
              //     if (res.code == 1) {
              //         this.vipServerList = res.data
              //     }
              // })
              // 获取所有学员信息
              getAll({}).then(res=>{
                if (res.code == 1) {
                    this.studentList = res.data
                }
              })
          })
      },
      methods: {
        getVipServerName(vipServerArray){
          if (vipServerArray && vipServerArray.length > 0) {
            return this.vipServerList.filter(item=>vipServerArray.includes(item.dicValue)).map(item=>item.dicName).join(', ')
          }
        },
        deleteRow(id){
            this.$confirm("请确认是否删除 ?", "警告  ! ! !", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                deletePurchaseCourses({id:id}).then(res => {
                    if (res.code == 1) {
                    this.$message.success(res.message)
                    // 重新获取数据
                    this.onSearch()
                    } else {
                    this.$message.error(res.message)
                    }
                })
            })
        },
        // 打开新增弹窗事件
        openAddModal() {
            this.$refs.addOrUpdateRef.openModal(null)
        },
        editRow(id){
            this.$refs.addOrUpdateRef.openModal(id)
        },
        // 搜索事件
        onSearch() {
            this.queryParams.pageNo = 1
            this.selectByPage()
        },
        // 分页查询事件
        selectByPage() {
            this.tableLoading = true
            selectByPage(this.queryParams).then((res) => {
                this.tableLoading = false
                if (res.code === 1) {
                    this.tableData = res.data.records
                    this.tableTotal = res.data.total
                }
            }).catch(() => {
                this.tableLoading = false
            })
        },
        // 表格高度
        countTableHeight(){
          let tableHeight = (window.innerHeight - 380) + 'px'
          // 计算搜索区域
          var searchContainerList =  document.getElementsByClassName("searchContainer")
          if (this.arrIsNotEmpty(searchContainerList) && this.arrIsNotEmpty(searchContainerList[0])) {
                    let searchContainerHeight = searchContainerList[0].offsetHeight
                    // header高度+面包屑高度+tab多标签页高度+tab多标签页底部margin的高度
                    let staticHeight = 60+38+30+15
                    if (this.amplify == true) {
                        // 放大后 , 就没有头部, 面包屑, tab多标签页了 , 预留70px给空白 , 并预留50px给分页
                        tableHeight = window.innerHeight - searchContainerHeight - 50 - 70 + 'px'
                    } else {
                        tableHeight = window.innerHeight - (staticHeight + searchContainerHeight) - 50 - 50 + 'px'
                    }
                    this.tableHeight = tableHeight
                }
        },
      },
      watch: {
          amplify(newValue,oldValue) {
              this.countTableHeight()
          },
      },
  }
</script>
<style lang="css" scoped>
  .multiRowText {
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      margin: auto;
  }
  :deep(.el-scrollbar) {
    text-align: left;
  }
</style>